package com.kobeliu.strategy;

import com.kobeliu.enums.BizCodeEnum;
import com.kobeliu.exception.BizException;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

public class CustomTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<String> {

    /**
     *
     * @param collection   ----  数据源的集合
     *                     分库时值为所有分片库的集合 ds0，ds1，ds2
     *                     分表时为 对应分库中所有分表的集合 short_link_0,short_link_1
     * @param preciseShardingValue  ---- 分片属性
     *                              logicTableName 逻辑表
     *                              columnName 分片键（字段）
     *                              value 从SQL中解析出来的分片键的值
     * @return
     */
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        // 0,1,2 sdsfs 0,1,2
        //code对应字段的最后一位字段代表表的位置
        String substring = preciseShardingValue.getValue().substring(preciseShardingValue.getValue().length()-1);

        String tableName = collection.iterator().next();

        return tableName+"_"+substring;

    }
}
